<?php
include("include/config.inc.php");
$itemIdVar    = $_REQUEST['itemIdVar'];
$waiterVar    = $_REQUEST['waiterVar'];
$tableVar     = $_REQUEST['tableVar'];
$tablePartVar = $_REQUEST['tablePartVar'];
$weight       = $_REQUEST['weight'];
$plusOrMinus  = $_REQUEST['plusOrMinusVar'];
$startTime    = date('H:i');
$selectItem   = "SELECT ordermaster.orderId 
                   FROM ordermaster
                  WHERE ordermaster.waiterId = '$waiterVar'
                    AND ordermaster.tableId = '$tableVar'
                    AND ordermaster.tablePart = '$tablePartVar'
                    AND ordermaster.orderstatus = 'L' ";
$orderRes    = mysql_query($selectItem);
$orderRow    = mysql_fetch_array($orderRes);
$orderNum    = mysql_num_rows($orderRes);
if($orderNum == 1)
{
  $orderIdNew=$orderRow['orderId'];
  $insertOrderedItems = "INSERT INTO ordereditems (orderId,itemId,quantity,weight,orderTime)
                              VALUES ('$orderIdNew','$itemIdVar','1','$weight','$startTime')";
  $insertOrderedItemsRes = mysql_query($insertOrderedItems);
}
else
{
  $insertOrder = "INSERT INTO  ordermaster (waiterId, tableId, tablePart, startTime, orderstatus)
                       VALUES('$waiterVar','$tableVar','$tablePartVar','$startTime','L')";
  $insertRes = mysql_query($insertOrder);
  $selectOederMaster = " SELECT orderId 
                           FROM ordermaster 
                          WHERE waiterId = '$waiterVar'
                            AND tableId = '$tableVar'
                            AND tablePart = '$tablePartVar'
                            AND orderstatus = 'L' ";
  $selectOederMasterRes = mysql_query($selectOederMaster);
  $selectOederMasterRow = mysql_fetch_array($selectOederMasterRes);
  $orderId = $selectOederMasterRow['orderId'];
  $insertOrderedItems = "INSERT INTO ordereditems (orderId,itemId,quantity,weight,orderTime)
                              VALUES ('$orderId','$itemIdVar','1','$weight','$startTime')";
  $insertOrderedItemsRes = mysql_query($insertOrderedItems);
}
?>
<table align="center" cellpadding="6">
  <!-- <tr>
    <td align="right" class="links"> 
      <a href="closeTables.php">Close Table</a>|<a href="checklogin.php">LogIn</a>
    </td>
  </tr> -->
  <tr>
    <td>
      <iframe src="currentTables.php?waiter=<?php echo $waiterVar; ?>&table=<?php echo $tableVar; ?>&orderId=<?php echo $orderId; ?>" width="500" height="350"></iframe>
    </td>
  </tr>
  <tr>
    <td>
      <iframe id="selectedTableIframe" src="selectedTable.php?waiterId=<?php echo $waiterVar; ?>&tableId=<?php echo $tableVar; ?>&tablePart=<?php echo $tablePartVar; ?>" width="500" height ="490"></iframe>
    </td>
  </tr>
</table>